package com.ctshk.rpc.order.custom.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.rpc.order.custom.req.order.CustomOrderTouristRefundAddReq;
import com.ctshk.rpc.order.custom.req.order.CustomOrderTouristRefundListReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

import java.util.Set;

/**
 * 旅客退款 provider
 * @author 叶冠峰
 * @since 2021-02-25
 */
public class CustomOrderTouristRefundProvider {

    public String selectListBySelective(@Param("page") IPage<CustomOrderTouristRefundListReq> page,
                                        @Param("req") CustomOrderTouristRefundListReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsoti.order_id, tsoti.schedule_id,tsoti.id AS touristId,tsotr.id AS refundId, " +
                "tsoti.cn_surname, tsoti.cn_name, " +
                "tsotr.refund_status, tsoti.total_paid_in, ");
        sb.append(
                "( select sum( temp2.refund_amount * temp2.exchange_rate ) FROM t_customized_order_tourist_refund temp1 " +
                        "LEFT JOIN t_customized_order_tourist_refund_detail temp2 " +
                        "ON temp1.id = temp2.refund_id where temp1.refund_status in (" +
                        NumberConstant.TWO.getValue()
                        + ", " + NumberConstant.THREE.getValue()
                        + ", " + NumberConstant.FIVE.getValue() + ") and temp1.tourist_id = tsoti.id) as refund_amount, ");
        sb.append(
                "( select sum( temp2.actual_refund_amount * temp2.exchange_rate ) FROM t_customized_order_tourist_refund temp1 " +
                        "LEFT JOIN t_customized_order_tourist_refund_detail temp2 " +
                        "ON temp1.id = temp2.refund_id where temp1.refund_status in (" +
                        NumberConstant.TWO.getValue()
                        + ", " + NumberConstant.THREE.getValue()
                        + ", " + NumberConstant.FIVE.getValue() + ") and temp1.tourist_id = tsoti.id) as actual_refund_amount ");
        sb.append("FROM t_customized_order_tourist_info tsoti ");
        sb.append("LEFT JOIN ( SELECT temp.id, temp.tourist_id, temp.refund_status, temp.received_amount, temp.refund_amount " +
                "FROM t_customized_order_tourist_refund temp WHERE temp.is_deleted = 0 GROUP BY temp.tourist_id ORDER BY temp.gmt_create desc ) tsotr ON tsoti.id = tsotr.tourist_id  ");
        sb.append("where tsoti.is_deleted = 0 AND tsoti.is_full_info = 1 ");
        sb.append("and tsoti.order_id = #{req.orderId} ");
        if (StringUtils.isNotBlank(req.getCondition1())) {
            sb.append("and ((tsoti.cn_surname || tsoti.cn_name) like concat('%',#{req.condition1},'%') " +
                    "OR (tsoti.en_name || tsoti.en_surname) like concat('%',#{req.condition1},'%'))");
        }
        return sb.toString();
    }

    public String scheduleRefundList(@Param("schedule_id") Long scheduleId){
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsoti.order_id, tsoti.schedule_id,tsoti.id AS touristId,tsotr.id AS refundId,tsotr.gmt_create,tsotr.create_id, " +
                "tsoti.cn_surname, tsoti.cn_name, " +
                "tsotr.refund_status,tsotr.refund_number, tsoti.total_paid_in,c.refund_type,c.refund_account,c.refund_reason,c.remark, ");
        sb.append(
                "( select sum( temp2.refund_amount ) FROM t_customized_order_tourist_refund temp1 " +
                        "LEFT JOIN t_customized_order_tourist_refund_detail temp2 " +
                        "ON temp1.id = temp2.refund_id where temp1.refund_status in (" +
                        NumberConstant.TWO.getValue()
                        + ", " + NumberConstant.THREE.getValue()
                        + ", " + NumberConstant.FIVE.getValue() + ") and temp1.tourist_id = tsoti.id) as refund_amount ");
        sb.append("FROM t_customized_order_tourist_info tsoti ");
        sb.append("LEFT JOIN ( SELECT temp.id,temp.refund_number, temp.tourist_id, temp.refund_status, temp.received_amount, temp.refund_amount,temp.gmt_create,temp.create_id " +
                "FROM t_customized_order_tourist_refund temp WHERE temp.is_deleted = 0 ORDER BY temp.gmt_create desc LIMIT 1 ) tsotr ON tsotr.tourist_id = tsoti.id ");
        sb.append("LEFT JOIN t_customized_order_tourist_refund_detail c ON tsotr.id = c.refund_id ");
        sb.append("where tsoti.schedule_id = #{schedule_id} AND tsoti.is_deleted = 0");
        return sb.toString();
    }

    public String refundList(@Param("order_id") Long orderId){
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsoti.order_id, tsoti.schedule_id,tsoti.id AS touristId,tsotr.id AS refundId, " +
                "tsoti.cn_surname, tsoti.cn_name, " +
                "tsotr.refund_status, tsoti.total_paid_in, ");
        sb.append(
                "( select sum( temp2.refund_amount ) FROM t_customized_order_tourist_refund temp1 " +
                        "LEFT JOIN t_customized_order_tourist_refund_detail temp2 " +
                        "ON temp1.id = temp2.refund_id where temp1.refund_status in (" +
                        NumberConstant.TWO.getValue()
                        + ", " + NumberConstant.THREE.getValue()
                        + ", " + NumberConstant.FIVE.getValue() + ") and temp1.tourist_id = tsoti.id) as refund_amount ");
        sb.append("FROM t_customized_order_tourist_info tsoti ");
        sb.append("LEFT JOIN ( SELECT temp.id, temp.tourist_id, temp.refund_status, temp.received_amount, temp.refund_amount " +
                "FROM t_customized_order_tourist_refund temp WHERE temp.is_deleted = 0 ORDER BY temp.gmt_create desc LIMIT 1 ) tsotr ON tsotr.tourist_id = tsoti.id ");
        sb.append("where tsoti.order_id = #{order_id} ");
        return sb.toString();
    }


    public String selectAddByRefundDetailTypeId(@Param("req") CustomOrderTouristRefundAddReq req,
                                                @Param("param1") Set<Long> param1) {
        StringBuilder sb = new StringBuilder();
        sb.append("select ");
        sb.append("tsotrd.refund_id, tsotrd.refund_detail_id ");
        sb.append("FROM t_customized_order_tourist_refund_detail tsotrd ");
        sb.append("LEFT JOIN t_customized_order_tourist_refund tsotr ON tsotr.id = tsotrd.refund_id ");
        sb.append("where tsotr.is_deleted = 0 ");
        sb.append("and tsotr.order_id = #{req.orderId} ");
        sb.append("and tsotr.tourist_id = #{req.touristId} ");
        if (param1.size() > 0) {
            sb.append(" and tsotrd.refund_detail_id in ( ");
            StringBuilder temp = new StringBuilder();
            int i = 0;
            for (Long id : param1) {
                i++;
                temp.append(id);
                if (i < param1.size()) {
                    temp.append(",");
                }
            }
            sb.append(temp);
            sb.append(" ) ");
        }
        return sb.toString();
    }

}
